package com.foucus.myfouces.Adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.foucus.myfouces.MVP.Model.NewestVedioListResp;
import com.foucus.myfouces.R;
import com.foucus.myfouces.Resource.Util.GliderUtil;
import com.foucus.myfouces.Resource.Util.StartActivity;
import com.foucus.myfouces.Resource.view.CircleImageView;

import java.util.List;

/**
 * Created by yf-06 on 2017/7/1.
 */

public class SquareNewVideoAdapter extends RecyclerView.Adapter<SquareNewVideoAdapter.MyHolder> {
    private RecyclerView mRecyclerView;

    private List<NewestVedioListResp.ResultBean.DataBean> newestVedioList;
    private Context mContext;

    private View VIEW_FOOTER;
    private View VIEW_HEADER;

    //Type
    private int TYPE_NORMAL = 1000;
    private int TYPE_HEADER = 1001;
    private int TYPE_FOOTER = 1002;
    private int total;

    public SquareNewVideoAdapter(List<NewestVedioListResp.ResultBean.DataBean> newestVedioList, Context mContext, int total) {
        this.newestVedioList = newestVedioList;
        this.mContext = mContext;
        this.total = total;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == TYPE_FOOTER) {

            return new MyHolder(VIEW_FOOTER);
        } else if (viewType == TYPE_HEADER) {
            return new MyHolder(VIEW_HEADER);
        } else {
            return new MyHolder(getLayout(R.layout.item_rcl_newvideo));
        }
    }


    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        if (!isHeaderView(position) && !isFooterView(position)) {
            if (haveHeaderView())
                position--;
            Log.e("ssss", "getThumb==>" + newestVedioList.get(position).getThumb());
            GliderUtil.bannerGlider(mContext, newestVedioList.get(position).getThumb(), holder.img_item_newVideo_icon);
            GliderUtil.bannerGlider(mContext, newestVedioList.get(position).getCreated_by().getThumb(), holder.cimg_item_newVideo_icon);
            holder.tv_item_newVideo_name.setText(newestVedioList.get(position).getCreated_by().getNickname());
            holder.tv_item_newVideo_text.setText("#" + newestVedioList.get(position).getGenre() + "#" +
                    newestVedioList.get(position).getName());
            final int finalPosition = position;
            holder.lly_item_newVideo_click.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    StartActivity.startVideoDetail((Activity) mContext, newestVedioList.get(finalPosition).getId())
                    ;

                }
            });

        }
    }

    @Override
    public int getItemCount() {
        int count = (newestVedioList == null ? 0 : newestVedioList.size());
        if (VIEW_FOOTER != null) {
            count++;
        }

        if (VIEW_HEADER != null) {
            count++;
        }
        return count;
    }

    @Override
    public int getItemViewType(int position) {
        if (isHeaderView(position)) {
            return TYPE_HEADER;
        } else if ((isFooterView(position))) {
            return TYPE_FOOTER;
        } else {
            return TYPE_NORMAL;
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        try {
            if (mRecyclerView == null && mRecyclerView != recyclerView) {
                mRecyclerView = recyclerView;
            }
            ifGridLayoutManager();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private View getLayout(int layoutId) {
        return LayoutInflater.from(mContext).inflate(layoutId, null);
    }

    public void addHeaderView(View headerView) {
        if (haveHeaderView()) {
            throw new IllegalStateException("hearview has already exists!");
        } else {

            //避免出现宽度自适应
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            headerView.setLayoutParams(params);
            VIEW_HEADER = headerView;
            ifGridLayoutManager();
            notifyItemInserted(0);

        }

    }

    public void addFooterView(View footerView) {
        if (haveFooterView()) {
            throw new IllegalStateException("footerView has already exists!");
        } else {
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            footerView.setLayoutParams(params);
            VIEW_FOOTER = footerView;
            ifGridLayoutManager();
            notifyItemInserted(getItemCount() - 1);
        }
    }

    public void deleteFoolterView() {
        VIEW_FOOTER = null;
        ifGridLayoutManager();
        notifyItemInserted(getItemCount() - 1);
    }

    private void ifGridLayoutManager() {
        if (mRecyclerView == null) return;
        final RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            final GridLayoutManager.SpanSizeLookup originalSpanSizeLookup =
                    ((GridLayoutManager) layoutManager).getSpanSizeLookup();
            ((GridLayoutManager) layoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return (isHeaderView(position) || isFooterView(position)) ?
                            ((GridLayoutManager) layoutManager).getSpanCount() :
                            1;
                }
            });
        }
    }

    private boolean haveHeaderView() {
        return VIEW_HEADER != null;
    }

    public boolean haveFooterView() {
        return VIEW_FOOTER != null;
    }

    private boolean isHeaderView(int position) {
        return haveHeaderView() && position == 0;
    }

    private boolean isFooterView(int position) {
        return haveFooterView() && position == getItemCount() - 1;
    }


    public static class MyHolder extends RecyclerView.ViewHolder {
        ImageView img_item_newVideo_icon;
        CircleImageView cimg_item_newVideo_icon;
        TextView tv_item_newVideo_name, tv_item_newVideo_text;
        LinearLayout lly_item_newVideo_click;

        public MyHolder(View itemView) {
            super(itemView);
            img_item_newVideo_icon = (ImageView) itemView.findViewById(R.id.img_item_newVideo_icon);
            cimg_item_newVideo_icon = (CircleImageView) itemView.findViewById(R.id.cimg_item_newVideo_icon);
            tv_item_newVideo_name = (TextView) itemView.findViewById(R.id.tv_item_newVideo_name);
            tv_item_newVideo_text = (TextView) itemView.findViewById(R.id.tv_item_newVideo_text);
            lly_item_newVideo_click = (LinearLayout) itemView.findViewById(R.id.lly_item_newVideo_click);
        }
    }
}
